package com.hospital.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hospital.util.DbUtil;

@WebServlet("/manage/CureServlet")
public class CureServlet extends HttpServlet {

	protected void doPost(HttpServletRequest request,HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		if ("add".equals(method)) {
			String orderid = request.getParameter("orderid").trim();
			String cases = request.getParameter("cases").trim();
			DbUtil dbutil = new DbUtil();
			String ypmc = request.getParameter("ypmc").trim();
			String ypsl = request.getParameter("ypsl").trim();
			String item = (String)request.getParameter("item").trim();
			int j = 0;
			j = dbutil.getIf(ypmc);
			int k = 0;
			k = dbutil.getSale(ypmc);
			if(Integer.parseInt(ypsl) > (j-k)){
				request.setAttribute("message", "该药品共进货"+j+",累计出售"+k+",剩余"+(j-k)+",您需要出售的数量为"+ypsl+"，操作失败！");
				request.getRequestDispatcher("/manage/cure_add2.jsp").forward(request, response);
			}else{
				String ypsalemoney = null;
				String sqlmoeny = "select * from t_medicinename where id = "+ ypmc;
				List<Map<String, Object>> listmoney = DbUtil.find(sqlmoeny);
				Map<String, Object> mapmoeny = null;
				String yapname = null;
				//从药品的名字表中得到药品的销售价格
				for (int i = 0; i < listmoney.size(); i++) {
					mapmoeny = (Map<String, Object>)listmoney.get(i);
					ypsalemoney = (String)mapmoeny.get("money");
					yapname = (String)mapmoeny.get("mname");
				}
				double ypsj = Double.parseDouble(ypsalemoney);
				String sumjg = String.valueOf(Integer.parseInt(ypsl) * ypsj);
				String sqlitemmoney = "select * from t_checklist where id = "+ item;
				List<Map<String, Object>> listitemmoney = DbUtil.find(sqlitemmoney);
				Map<String, Object> mapitemmoney = null;
				String itemmoney = null;
				for (int i = 0; i < listmoney.size(); i++) {
					mapitemmoney = (Map<String, Object>)listitemmoney.get(i);
					itemmoney = (String)mapitemmoney.get("price");
				}
				String sql = "insert into t_cure(orderid, cases, drug, drugmoney, item, itemmoney) values('" 
						+ orderid + "', '" + cases + "','" + yapname + "'," + ypsj + ",'" + item + "'," + itemmoney + ")";
				DbUtil.add_modify(sql);
				//将医生开的药进行处理
				String sqlyp = "insert into t_sale(ypmc,ypsj,ypsl,sumjg,xsrq) values('"
						+ ypmc +"', '" + ypsj + "', '" + ypsl + "','" + sumjg + "', '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "')";
				DbUtil.add_modify(sqlyp);
				String sqlsy = "update t_stroage set ypsysl = '" + (j-k-Integer.parseInt(ypsl)) + "' where ypmc = " + ypmc;
				DbUtil.add_modify(sqlsy);
				String sqlcheckmoney = "insert into t_check_money(checkname, money, addtime) values('"
						+ item + "', '" + itemmoney + "','" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "')";
				DbUtil.add_modify(sqlcheckmoney);
				//将医生开的检查单进行处理
				double totalprices = ypsj + Double.parseDouble(itemmoney);
				String sql_name = "select name from t_order where oid = '" + orderid + "'";
				String name = DbUtil.getString(sql_name);
				String sql_money = "update t_patient_info set money = money - '" + totalprices + "' where account = '" + name + "'";
				DbUtil.add_modify(sql_money);
				String Inhospitable = request.getParameter("Inhospitable");
				String sql_Inhospitable = "update t_patient_info set Inhospitable = '" + Inhospitable + "' where account = '" + name + "'";
				String sql_InhospitableOrder = "update t_order set Inhospitable = '" + Inhospitable + "' where oid = '" + orderid + "'";
				DbUtil.add_modify(sql_Inhospitable);
				DbUtil.add_modify(sql_InhospitableOrder);

				request.getRequestDispatcher("/manage/cure_manage.jsp").forward(request, response);
			}
		} else if ("del".equals(method)) {
			String[] id = request.getParameterValues("selectFlag");
			DbUtil.del("t_cure", id);
			request.getRequestDispatcher("/manage/cure_manage.jsp").forward(request, response);
		} else if ("modify".equals(method)) {
			int id = Integer.parseInt(request.getParameter("id"));
			String orderid = request.getParameter("orderid").trim();
			String cases = request.getParameter("cases").trim();
			DbUtil dbutil = new DbUtil();
			String ypmc = request.getParameter("ypmc").trim();
			String ypsl = request.getParameter("ypsl").trim();
			String item = (String)request.getParameter("item").trim();
			String sql_old = "select * from t_cure where id = " + id;
			Map<String, Object> map  = (Map<String, Object>)DbUtil.find(sql_old).get(0);
			double old_toatalprices = (Double)map.get("drugmoney") + (Double)map.get("itemmoney");
			int j = 0;
			j = dbutil.getIf(ypmc);
			int k = 0;
			k = dbutil.getSale(ypmc);
			if(Integer.parseInt(ypsl) > (j-k)){
				request.setAttribute("message", "该药品共进货"+j+",累计出售"+k+",剩余"+(j-k)+",您需要出售的数量为"+ypsl+"，操作失败！");
				request.getRequestDispatcher("/manage/cure_add2.jsp").forward(request, response);
			}else{
				String ypsalemoney = null;
				String sqlmoeny = "select * from t_medicinename where id = "+ ypmc;
				List<Map<String, Object>> listmoney = DbUtil.find(sqlmoeny);
				Map<String, Object> mapmoeny = null;
				String yapname = null;
				//从药品的名字表中得到药品的销售价格
				for (int i = 0; i < listmoney.size(); i++) {
					mapmoeny = (Map<String, Object>)listmoney.get(i);
					ypsalemoney = (String)mapmoeny.get("money");
					yapname = (String)mapmoeny.get("mname");
				}
				double ypsj = Double.parseDouble(ypsalemoney);
				String sumjg = String.valueOf(Integer.parseInt(ypsl) * ypsj);
				String sqlitemmoney = "select * from t_checklist where id = "+ item;
				List<Map<String, Object>> listitemmoney = DbUtil.find(sqlitemmoney);
				Map<String, Object> mapitemmoney = null;
				String itemmoney = null;
				for (int i = 0; i < listmoney.size(); i++) {
					mapitemmoney = (Map<String, Object>)listitemmoney.get(i);
					itemmoney = (String)mapitemmoney.get("price");
				}
				String sql = "update t_cure set cases='"+cases + "', drug = '" + yapname + "', drugmoney = " + ypsj 
						+ ", item = '" + item + "',itemmoney = " + itemmoney + "where orderid = '" + orderid +"'";
				DbUtil.add_modify(sql);
				//将医生开的药进行处理
				String sqlyp = "insert into t_sale(ypmc,ypsj,ypsl,sumjg,xsrq) values('"
						+ ypmc +"', '" + ypsj + "', '" + ypsl + "','" + sumjg + "', '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "')";
				DbUtil.add_modify(sqlyp);
				String sqlsy = "update t_stroage set ypsysl = '" + (j-k-Integer.parseInt(ypsl)) + "' where ypmc = " + ypmc;
				DbUtil.add_modify(sqlsy);
				String sqlcheckmoney = "insert into t_check_money(checkname, money, addtime) values('"
						+ item + "', '" + itemmoney + "','" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()) + "')";
				DbUtil.add_modify(sqlcheckmoney);
				//将医生开的检查单进行处理
				double totalprices = ypsj + Double.parseDouble(itemmoney);
				String sql_name = "select name from t_order where oid = '" + orderid + "'";
				String name = DbUtil.getString(sql_name);
				String Inhospitable = request.getParameter("Inhospitable");
				String sql_Inhospitable = "update t_patient_info set Inhospitable = '" + Inhospitable + "' where account = '" + name + "'";
				String sql_InhospitableOrder = "update t_order set Inhospitable = '" + Inhospitable + "' where oid = '" + orderid + "'";
				DbUtil.add_modify(sql_Inhospitable);
				DbUtil.add_modify(sql_InhospitableOrder);
				//修改了就诊单就是说明要在病患本身的充值卡上加上老的价格再减去新的价格
				double new_totalprices = totalprices;
				double totalprices_really = new_totalprices - old_toatalprices;
				String sql_name_patient = "select name from t_order where oid = '" + orderid + "'";
				String name_patient = DbUtil.getString(sql_name_patient);
				String sql_money_new = "update t_patient_info set money = money - '" + totalprices_really + "' where account = '" + name_patient + "'";
				DbUtil.add_modify(sql_money_new);
				request.getRequestDispatcher("/manage/cure_manage.jsp").forward(request, response);
			}
		}
	}
}